package com.snj.test;

import java.util.*;

/**
 * @author yujie
 * @data 2024/11/15 11:22
 */
public class Main4 {
    public static void main(String[] args) {
        Integer start = 5;
        Integer end = 13;
        // 示例的 intList
        List<Integer> intList = new ArrayList<>();
        intList.addAll(Arrays.asList(4, 4, 4, 2, 14));

        List<Integer> flagList = countPersonFlag(end, intList);

        List<Integer> sumTest = countSumFlag(end, intList);
        System.out.println(flagList);  // 输出结果
        System.out.println(sumTest);  // 输出结果
        System.out.println("-------------------");


        Integer num = start / 2;  // 自动向下取整

        for (int j = 0; j < sumTest.size(); j++) {
            Integer temp = sumTest.get(j);

            if (temp <= num) {
                if (j == 0) {
                    if (j + 1 >= sumTest.size()) {
                        sumTest.set(j, start);
                    } else {
                        flagList.set(j + 1, flagList.get(j) + 1);
                        flagList.remove(j);
                        sumTest.remove(j);
                        j--; // 因为删除了元素，需要减少j
                    }
                } else {
                    flagList.set(j - 1, flagList.get(j - 1) + 1);
                    flagList.remove(j);
                    sumTest.remove(j);
                    j--; // 因为删除了元素，需要减少j
                }
            } else if (temp < start) {
                sumTest.set(j, start);
            } else {
                boolean oddFlag = isOdd(temp);
                if (!oddFlag) {
                    if (temp > end) {
                        sumTest.set(j, temp);
                    } else {
                        sumTest.set(j, temp + 1);
                    }
                }
            }
        }

        System.out.println(flagList);  // 输出结果
        System.out.println(sumTest);  // 输出结果

        Map<String, List<Integer>> map = new HashMap<>();
        map.put("技术1组", Arrays.asList(1, 1));
        map.put("技术2组", Arrays.asList(2, 2));
        map.put("技术3组", Arrays.asList(3, 3));
        map.put("技术4组", Arrays.asList(4, 4));
        map.put("技术5组", Arrays.asList(5, 5));
//        map.put("技术6组", Arrays.asList(6, 6));
//        map.put("技术7组", Arrays.asList(7, 7));
//        map.put("技术8组", Arrays.asList(8, 8));

        Map<String, List<Integer>> temp = new HashMap<>();
        int flag = 0;

        int prevIndex = 0;

        for (int j = 0; j < flagList.size(); j++) {
            int index = flagList.get(j);
            List<Integer> arrayList = new ArrayList<>();

            for (int k = prevIndex + 1; k <= index; k++) {
                flag++;
                String keyName = "技术" + flag + "组";
                List<Integer> list = map.getOrDefault(keyName, Collections.emptyList());
                arrayList.addAll(list);
            }

            prevIndex = index;
            String name = "商务" + (temp.size() + 1) + "组";
            temp.put(name, arrayList);
        }

        System.out.println(temp);
    }

    public static boolean isOdd(int number) {
        return number % 2 != 0;
    }

    public static List<Integer> countPersonFlag(Integer end, List<Integer> list) {
        Integer sum = 0;
        int indexFlag = 1;
        List<Integer> indices = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
            if (sum >= end || (i < list.size() - 1 && sum + list.get(i + 1) > end)) {
                // 记录当前索引 + 1
                indices.add(i + 1);
                // 将 sum 归零
                sum = 0;
            }
        }
        if (sum > 0) {
            // 如果循环结束时 sum 还有值，记录最后一个索引
            indices.add(list.size());

        }
        return indices;
    }


    public static List<Integer> countSumFlag(Integer end, List<Integer> list) {
        Integer sum = 0;
        List<Integer> values = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
            if (sum >= end || (i < list.size() - 1 && sum + list.get(i + 1) > end)) {
                // 记录当前的累加值
                values.add(sum);
                sum = 0;
            }
        }
        if (sum > 0) {
            // 记录最后一个累加值
            values.add(sum);
        }


        return values;
    }
}
